<xsl:stylesheet version="1.1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2000/svg"
                xmlns:xlink="http://www.w3.org/1999/xlink"
                xmlns:random="http://exslt.org/random"
                xmlns:math="http://exslt.org/math"
                xmlns:exsl="http://exslt.org/common"
                xmlns:temp="http://temp.org/temp"
                extension-element-prefixes="exsl"
                exclude-result-prefixes="random math exsl">
            
 	<xsl:param name="card-width">181</xsl:param>            
 	<xsl:param name="card-height">256</xsl:param>
 	<xsl:param name="card-hspacing">3</xsl:param>
 	<xsl:param name="card-vspacing">3</xsl:param>
 	<xsl:param name="max-cards-across">4</xsl:param>            
 	<xsl:param name="major-spacing">8</xsl:param>
 	<xsl:param name="minor-spacing">4</xsl:param>
 	<xsl:param name="cost-column-width">14</xsl:param>
 	<xsl:param name="cost-row-height">14</xsl:param>

  	<xsl:output method="xml"/>
  
  	<xsl:template match="/">
  		<xsl:variable name="num-cards">
  			<xsl:apply-templates select="game/deck" mode="count-cards"/>
  		</xsl:variable>
  		<xsl:variable name="num-cols" select="$max-cards-across"/>
  		<xsl:variable name="num-rows" select="ceiling($num-cards div $num-cols)"/>
    	<xsl:variable name="width" select="$num-cols * $card-width + ($num-cols - 1) * $card-hspacing + 2"/>
    	<xsl:variable name="height" select="$num-rows * $card-height + ($num-rows - 1) * $card-vspacing + 2"/>

		<svg width="{$width}" height="{$height}" viewBox="0 0 {$width} {$height}" version="1.1">
	    	<g>
	    		<xsl:apply-templates select="game/deck" mode="render-cards"/>
	        </g>
		</svg>	  
  	</xsl:template>
  	
  	<xsl:template match="deck" mode="render-cards">
  		<xsl:param name="item-index">1</xsl:param>
  		<xsl:param name="item-count" select="count(item)"/>
  		<xsl:param name="card-count">0</xsl:param>
  		<xsl:param name="offset" select="count(knowledge)"/>
  		<xsl:choose>
  			<xsl:when test="$item-count >= $item-index">
  				<xsl:apply-templates select="item[$item-index]" mode="render-cards">
  					<xsl:with-param name="offset" select="$card-count + $offset"/>
  				</xsl:apply-templates>
  				<xsl:variable name="num-cards">
  					<xsl:apply-templates select="item[$item-index]" mode="count-cards"/>
  				</xsl:variable>
  				<xsl:apply-templates select="." mode="render-cards">
  					<xsl:with-param name="item-index" select="$item-index + 1"/>
  					<xsl:with-param name="item-count" select="$item-count"/>
  					<xsl:with-param name="card-count" select="$card-count + $num-cards"/>
  				</xsl:apply-templates>
  			</xsl:when>
  			<xsl:otherwise>
  				<!-- render the knowledge cards at the start -->
  				<xsl:apply-templates select="knowledge" mode="render-cards">
  					<xsl:with-param name="offset">0</xsl:with-param>
  				</xsl:apply-templates>
  			</xsl:otherwise>
  		</xsl:choose>
  	</xsl:template>

  	<xsl:template match="knowledge" mode="render-cards">
  		<xsl:param name="offset">0</xsl:param>
  		<xsl:message>offset <xsl:value-of select="$offset"/></xsl:message>
		<xsl:apply-templates select="knowledge-spec" mode="render-card">
			<xsl:with-param name="offset" select="$offset + position() - 1"/>
		</xsl:apply-templates>  		
  	</xsl:template>
  	
  	
  	<xsl:template match="item" mode="render-cards">
  		<xsl:param name="offset">0</xsl:param>
  		<xsl:param name="index">1</xsl:param>
  		<xsl:param name="count" select="@quantity"/>
  		<xsl:if test="$count >= $index">
  			<xsl:apply-templates select="item-spec" mode="render-cards">
  				<xsl:with-param name="offset" select="$offset"/>
  			</xsl:apply-templates>
  			<xsl:variable name="num-cards">
  				<xsl:apply-templates select="item-spec" mode="count-cards"/>
  			</xsl:variable>
  			<xsl:apply-templates select="." mode="render-cards">
  				<xsl:with-param name="offset" select="$offset + $num-cards"/>
  				<xsl:with-param name="index" select="$index + 1"/>
  				<xsl:with-param name="count" select="$count"/>
  			</xsl:apply-templates>
  		</xsl:if>
  	</xsl:template>
  	
  	<xsl:template match="item-spec" mode="render-cards">
  		<xsl:param name="offset">0</xsl:param>
  		<xsl:apply-templates select="." mode="render-card">
  			<xsl:with-param name="offset" select="$offset"/>
  		</xsl:apply-templates>
  		<!-- TODO break by application? -->
 		<xsl:apply-templates select="application" mode="render-cards">
  			<xsl:with-param name="offset" select="$offset + 1"/>
  		</xsl:apply-templates>
  	</xsl:template>
  	
  	<xsl:template match="application" mode="render-cards">
  		<xsl:param name="offset">0</xsl:param>
  		<xsl:param name="index" select="count(action)"/>
  		<xsl:variable name="action-quantity" select="action[$index]/@quantity"/>
  		<xsl:if test="$index > 0">
  			<xsl:apply-templates select="action[$index]" mode="render-cards">
  				<xsl:with-param name="offset" select="$offset"/>
  			</xsl:apply-templates>
  			<xsl:apply-templates select="." mode="render-cards">
  				<xsl:with-param name="offset" select="$offset + $action-quantity"/>
  				<xsl:with-param name="index" select="$index - 1"/>
  			</xsl:apply-templates>
  		</xsl:if>
  	</xsl:template>
  	
  	<xsl:template match="action" mode="render-cards">
  		<xsl:param name="offset">0</xsl:param>
  		<xsl:param name="index" select="@quantity"/>
  		<xsl:if test="$index > 0">
			<xsl:apply-templates select="action-spec" mode="render-card">
  				<xsl:with-param name="offset" select="$offset"/>			
			</xsl:apply-templates>
  			<xsl:apply-templates select="." mode="render-cards">
  				<xsl:with-param name="index" select="$index - 1"/>
  				<xsl:with-param name="offset" select="$offset + 1"/>
  			</xsl:apply-templates>
  		</xsl:if>
  	</xsl:template>
  	
  	<xsl:template match="*" mode="render-card">
  		<xsl:param name="offset">0</xsl:param>
 		<xsl:variable name="x" select="($offset mod $max-cards-across) * ($card-width + $card-hspacing)"/>
 		<xsl:variable name="y" select="floor($offset div $max-cards-across) * ($card-height + $card-vspacing)"/>
		<g transform="translate({$x},{$y})">
	  		<rect x="0" y="0" width="{$card-width}" height="{$card-height}" fill="white" stroke="black" stroke-width="2" rx="5" ry="5"/>
	  		<xsl:apply-templates select="." mode="render-card-content"/>
	  	</g>
  	</xsl:template>
  	
  	<xsl:template match="item-spec | knowledge-spec" mode="render-card-content">
		<xsl:variable name="picture-width" select="$card-width - $major-spacing * 2"/>
		<xsl:variable name="picture-height" select="$picture-width"/>
		<xsl:variable name="picture-inset" select="$major-spacing"/>
		<!-- scale the contained image -->
		<rect x="{$picture-inset}" y="{$picture-inset}" width="{$picture-width}" height="{$picture-height}" fill="white" stroke="black" stroke-width="1"/>
		<xsl:apply-templates select="image" mode="render-image">
			<xsl:with-param name="x" select="$picture-inset"/>
			<xsl:with-param name="y" select="$picture-inset"/>
			<xsl:with-param name="image-width" select="$picture-width"/>
			<xsl:with-param name="image-height" select="$picture-height"/>
		</xsl:apply-templates>
		<!-- 
		<xsl:if test="image">
			<xsl:variable name="scale-x" select="$picture-width div image/@width"/>
			<xsl:variable name="scale-y" select="$picture-height div image/@height"/>
			<xsl:variable name="scale">
				<xsl:choose>
					<xsl:when test="$scale-x > $scale-y">
						<xsl:value-of select="$scale-y"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$scale-x"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:variable name="scaled-image-width" select="$scale * image/@width"/>
			<xsl:variable name="scaled-image-height" select="$scale * image/@height"/>
			<g transform="translate({$picture-inset + ($picture-width - $scaled-image-width) div 2}, {$picture-inset + ($picture-height - $scaled-image-height) div 2})">
				<g transform="scale({$scale})">
					<xsl:copy-of select="image/*"/>
				</g>
			</g>
		</xsl:if>
		 -->
		<xsl:variable name="text-offset" select="$picture-inset * 2 + $picture-height"/>
		<xsl:apply-templates select="description" mode="render-description">
			<xsl:with-param name="x" select="$picture-inset"/>
			<xsl:with-param name="y" select="$text-offset"/>
			<xsl:with-param name="width" select="$card-width - $picture-inset * 2"/>
			<xsl:with-param name="height" select="$card-height - $text-offset"/>
			<xsl:with-param name="title" select="@name"/>
		</xsl:apply-templates>
		
		<g transform="translate({$major-spacing},0)">
			<xsl:apply-templates select="." mode="render-flowchart">
				<xsl:with-param name="baseline-y" select="$card-height - $major-spacing"/>
	  		</xsl:apply-templates>			
		</g>			
  	</xsl:template>
  	
  	<xsl:template match="action-spec" mode="render-card-content">
  		<xsl:variable name="mode-count" select="count(mode)"/>
  		<xsl:choose>
  			<xsl:when test="$mode-count = 1">
  				<g transform="translate({$major-spacing}, {$major-spacing})">
  					<xsl:apply-templates select="mode" mode="render-card-content-portrait">
						<xsl:with-param name="width" select="$card-width - $major-spacing * 2"/>
						<xsl:with-param name="height" select="$card-height - $major-spacing * 2"/>
  					</xsl:apply-templates>
  				</g>
  			</xsl:when>
  			<xsl:otherwise>
  				<!-- assume 2 -->
  				<xsl:variable name="part-height" select="$card-height div 2"/>
  				<g transform="translate({$major-spacing}, {$major-spacing})">
					<xsl:apply-templates select="mode[@type='primary']" mode="render-card-content-landscape">
						<xsl:with-param name="width" select="$card-width - $major-spacing * 2"/>
						<xsl:with-param name="height" select="$part-height - ($major-spacing + $minor-spacing)"/>
					</xsl:apply-templates>
				</g>
  				<line x1="0" y1="{$part-height}" x2="{$card-width}" y2="{$part-height}" stroke="black" stroke-width="1" stroke-dasharray="5, 3, 9, 2"/>
  				<g transform="translate({$card-width - $major-spacing}, {$part-height + $minor-spacing})">
	  				<g transform="rotate(90)">
	  					<xsl:apply-templates select="mode[@type='secondary']" mode="render-card-content-portrait">
	  						<xsl:with-param name="width" select="$part-height - ($major-spacing + $minor-spacing)"/>
	  						<xsl:with-param name="height" select="$card-width - $major-spacing * 2"/>
	  					</xsl:apply-templates>
	  				</g>
  				</g>
  			</xsl:otherwise>
  		</xsl:choose>
  	</xsl:template>
  	
  	<xsl:template match="mode" mode="render-card-content-landscape">
  		<xsl:param name="width" select="$card-width"/>
  		<xsl:param name="height" select="$card-height"/>
  		
  		<xsl:variable name="image-height" select="($height div 2) - $major-spacing div 2"/>
  		<xsl:variable name="image-width" select="$image-height"/>
  		
  		<rect x="0" y="0" width="{$image-width}" height="{$image-height}" stroke="black" fill="white" stroke-width="1"/>
  		
  		<xsl:apply-templates select="description" mode="render-description">
  			<xsl:with-param name="x" select="$image-width + $major-spacing"/>
  			<xsl:with-param name="y" select="0"/>
  			<xsl:with-param name="width" select="$width - $image-width - $major-spacing"/>
  			<xsl:with-param name="height" select="$height"/>
  			<xsl:with-param name="title" select="@name"/>
  			<xsl:with-param name="icon-type" select="@range"/>  			
  		</xsl:apply-templates>
  		
		<xsl:apply-templates select="." mode="render-flowchart">
			<xsl:with-param name="baseline-y" select="$height"/>
  		</xsl:apply-templates>
  		
  		
  	</xsl:template>
  	
  	<xsl:template match="mode" mode="render-card-content-portrait">
  		<xsl:param name="width" select="$card-width"/>
  		<xsl:param name="height" select="$card-height"/>
  		
  		<xsl:variable name="image-width" select="($width div 2) - $major-spacing div 2"/>
  		<xsl:variable name="image-height" select="$image-width"/>
  		
  		<rect x="0" y="0" width="{$image-width}" height="{$image-height}" stroke="black" fill="white" stroke-width="1"/>
  		
  		<xsl:apply-templates select="description" mode="render-description">
  			<xsl:with-param name="y" select="$image-height + $major-spacing"/>
  			<xsl:with-param name="width" select="$width"/>
  			<xsl:with-param name="height" select="$height - $image-height - $major-spacing"/>
  			<xsl:with-param name="title" select="@name"/>
  			<xsl:with-param name="icon-type" select="@range"/>
  		</xsl:apply-templates>
  		
		<xsl:apply-templates select="." mode="render-flowchart">
			<xsl:with-param name="baseline-y" select="$height"/>		
  		</xsl:apply-templates>
  		
  	</xsl:template>
  	
  	<xsl:template match="*" mode="render-flowchart">
  		<xsl:param name="baseline-y">0</xsl:param>
  		
  		<xsl:variable name="grid-string">
  			<xsl:apply-templates select="." mode="build-columns"/>
  		</xsl:variable>
  		<xsl:variable name="grid" select="exsl:node-set($grid-string)"/>
  		
  		<xsl:variable name="mode" select="."/>
  		
  		<xsl:if test="count($grid/temp:grid/temp:entry) > 0">
  		
	  		<xsl:variable name="rows" select="math:max($grid/temp:grid/temp:entry/@row) + 1"/>
	  		<xsl:variable name="cols" select="math:max($grid/temp:grid/temp:entry/@col) + 1"/>
	  		<xsl:variable name="width" select="$cols * $cost-column-width + ($cols - 1) * $minor-spacing"/>
	  		<xsl:variable name="height" select="$rows * $cost-row-height + ($rows - 1) * $minor-spacing"/>
	
			<g transform="translate(0, {$baseline-y - $height})">
			
				<xsl:for-each select="step">
					<xsl:variable name="step-id" select="@id"/>
		  			<xsl:variable name="to-entry" select="$grid/temp:grid/temp:entry[@step-id=$step-id]"/>
					<xsl:variable name="to-x" select="$to-entry/@col * ($cost-column-width * 2) + $cost-column-width div 2"/>
					<xsl:variable name="to-y" select="$height - ($to-entry/@row + 1) * $cost-row-height - $to-entry/@row * $minor-spacing + $cost-row-height div 2"/>
		  			
		  			<xsl:for-each select="depend">
		  				
		  				<xsl:variable name="from-step-id" select="string(.)"/>
		  				<xsl:variable name="from-entry" select="$grid/temp:grid/temp:entry[@step-id=$from-step-id]"/>
		  				<xsl:message><xsl:value-of select="$from-step-id"/> ->  <xsl:value-of select="$step-id"/></xsl:message>
		  				<xsl:variable name="from-x" select="$from-entry/@col * ($cost-column-width * 2) + $cost-column-width div 2"/>
		  				<xsl:variable name="from-y" select="$height - ($from-entry/@row + 1) * $cost-row-height - $from-entry/@row * $minor-spacing + $cost-row-height div 2"/>
		  				<xsl:message>(<xsl:value-of select="$from-x"/>,<xsl:value-of select="$from-y"/>)->(<xsl:value-of select="$to-x"/>,<xsl:value-of select="$to-y"/>)</xsl:message>
		  				<line x1="{$from-x}" y1="{$from-y}" x2="{$to-x}" y2="{$to-y}" stroke="black"/>
		  				
		   			</xsl:for-each>
					
				</xsl:for-each>
		  		
		  		<xsl:for-each select="step">
		  			<xsl:variable name="step-id" select="@id"/>
		  			<xsl:variable name="entry" select="$grid/temp:grid/temp:entry[@step-id=$step-id]"/>
		  			
					<xsl:variable name="resource-id">
						<xsl:choose>
							<xsl:when test="@add">
								<xsl:value-of select="@add"/>
							</xsl:when>
							<xsl:when test="@subtract">
								<xsl:value-of select="@subtract"/>
							</xsl:when>
							<xsl:when test="@action">
								<xsl:value-of select="@action"/>
							</xsl:when>
						</xsl:choose>
					</xsl:variable>
		  			
					<xsl:apply-templates select="/game" mode="render-resource">
						<xsl:with-param name="x" select="$entry/@col * ($cost-column-width * 2)"/>
						<xsl:with-param name="y" select="$height - ($entry/@row + 1) * $cost-row-height - $entry/@row * $minor-spacing"/>
						<xsl:with-param name="resource-id" select="$resource-id"/>
						<xsl:with-param name="quantity" select="@quantity"/>
						<xsl:with-param name="width" select="$cost-column-width"/>
						<xsl:with-param name="height" select="$cost-row-height"/>
					</xsl:apply-templates>
		  			
		  		</xsl:for-each>
	  		
		  	</g>
  		</xsl:if>
  	</xsl:template>
  	
  	<xsl:template match="*" mode="build-columns">
  		<xsl:variable name="cols-string">
	  		<temp:grid>
	  			<xsl:for-each select="step">
	  				<xsl:variable name="col">
  						<xsl:apply-templates select=".." mode="max-step-dependencies">
  							<xsl:with-param name="step-id" select="@id"/>
	  					</xsl:apply-templates>
	  				</xsl:variable>
	  				<xsl:variable name="max-col">
	  					<xsl:apply-templates select=".." mode="min-max-child-step-dependencies">
	  						<xsl:with-param name="step-id" select="@id"/>
	  					</xsl:apply-templates>
	  				</xsl:variable>
	  				<xsl:variable name="final-col">
	  					<xsl:choose>
	  						<xsl:when test="$max-col > $col">
	  							<xsl:value-of select="$max-col - 1"/>
	  						</xsl:when>
	  						<xsl:otherwise>
	  							<xsl:value-of select="$col"/>
	  						</xsl:otherwise>
	  					</xsl:choose>
	  				</xsl:variable>
	  				<xsl:message><xsl:value-of select="@id"/> : <xsl:value-of select="$col"/>/<xsl:value-of select="$max-col"/></xsl:message>
	  				<temp:step id="{@id}" pos="{position()}" col="{$final-col}"/>
	  			</xsl:for-each>
	  		</temp:grid>
  		</xsl:variable>
  		<xsl:variable name="cols" select="exsl:node-set($cols-string)"/>
  		
  		<temp:grid>
  			<xsl:for-each select="step">
  				<xsl:variable name="step-id" select="@id"/>
  				<xsl:variable name="position" select="position()"/>
  				<xsl:variable name="col" select="$cols/temp:grid/temp:step[@id=$step-id]/@col"/>
  				<xsl:variable name="row" select="count($cols/temp:grid/temp:step[@col=$col and $position>@pos])"/>
  				<temp:entry step-id="{$step-id}" col="{$col}" row="{$row}"/>
  			</xsl:for-each>
  		</temp:grid>
  	</xsl:template>
  	
  	<xsl:template match="*" mode="max-step-dependencies">
  		<xsl:param name="step-id"/>
  		<xsl:param name="max">0</xsl:param>
  		<xsl:param name="count" select="count(step[@id=$step-id]/depend)"/>
  		<xsl:param name="index" select="$count"/>
  		<xsl:choose>
  			<xsl:when test="$index > 0">
  				<xsl:variable name="dependency-max">
  					<xsl:apply-templates select="." mode="max-step-dependencies">
  						<xsl:with-param name="step-id" select="step[@id=$step-id]/depend[$index]"/>
  					</xsl:apply-templates>
  				</xsl:variable>
  				<xsl:variable name="new-max">
  					<xsl:choose>
  						<xsl:when test="$dependency-max >= $max">
  							<xsl:value-of select="$dependency-max + 1"/>
  						</xsl:when>
  						<xsl:otherwise>
  							<xsl:value-of select="$max"/>
  						</xsl:otherwise>
  					</xsl:choose>
  				</xsl:variable>
  				<xsl:apply-templates select="." mode="max-step-dependencies">
  					<xsl:with-param name="max" select="$new-max"/>
  					<xsl:with-param name="step-id" select="$step-id"/>
  					<xsl:with-param name="index" select="$index - 1"/>
  					<xsl:with-param name="count" select="$count"/>
  				</xsl:apply-templates>
  			</xsl:when>
  			<xsl:otherwise>
  				<xsl:value-of select="$max"/>
  				<xsl:message><xsl:value-of select="$step-id"/>=<xsl:value-of select="$max"/></xsl:message>
  				
  			</xsl:otherwise>
  		</xsl:choose>

  	</xsl:template> 
  	
  	<xsl:template match="*" mode="min-max-child-step-dependencies">
  		<xsl:param name="step-id"/>
		<xsl:param name="min"/>
  		<xsl:param name="count" select="count(step[depend=$step-id])"/>
  		<xsl:param name="index" select="$count"/>
  		
  		<xsl:choose>
  			<xsl:when test="$index > 0">
  				<xsl:variable name="child-step-id" select="step[depend=$step-id][$index]/@id"/>
  				<xsl:variable name="max">
  					<xsl:apply-templates select="." mode="max-step-dependencies">
  						<xsl:with-param name="step-id" select="$child-step-id"/>
  					</xsl:apply-templates>
  				</xsl:variable>
  				<!-- 
  				<xsl:message><xsl:value-of select="$step-id"/> max for <xsl:value-of select="$child-step-id"/> is <xsl:value-of select="$max"/></xsl:message>
  				 -->
  				<xsl:variable name="new-min">
  					<xsl:choose>
  						<xsl:when test="$min and ($max > $min)">
  							<xsl:value-of select="$min"/>
  						</xsl:when>
  						<xsl:otherwise>
  							<xsl:value-of select="$max"/>
  						</xsl:otherwise>
  					</xsl:choose>
  				</xsl:variable>
  				<xsl:apply-templates select="." mode="min-max-child-step-dependencies">
  					<xsl:with-param name="step-id" select="$step-id"/>
  					<xsl:with-param name="min" select="$new-min"/>
  					<xsl:with-param name="count" select="$count"/>
  					<xsl:with-param name="index" select="$index - 1"/>
  				</xsl:apply-templates>
  			</xsl:when>
  			<xsl:otherwise>
  				<xsl:choose>
  					<xsl:when test="$min">
  						<xsl:message>returning non 0</xsl:message>
  						<xsl:value-of select="$min"/>
  					</xsl:when>
  					<xsl:otherwise>0</xsl:otherwise>
  				</xsl:choose>
  			</xsl:otherwise>
  		</xsl:choose>
  		
  	</xsl:template>
	
	<xsl:template match="game" mode="render-resource">
		<xsl:param name="x"/>
		<xsl:param name="y"/>
		<xsl:param name="resource-id"/>
		<xsl:param name="quantity"/>
		<xsl:param name="width"/>
		<xsl:param name="height"/>
				
		<xsl:apply-templates select="/game/resource[@id=$resource-id]/icon" mode="render-image">
			<xsl:with-param name="x" select="$x"/>
			<xsl:with-param name="y" select="$y"/>
			<xsl:with-param name="image-width" select="$width"/>
			<xsl:with-param name="image-height" select="$height"/>
		</xsl:apply-templates>
		<flowText xmlns="http://xml.apache.org/batik/ext">
			<flowRegion vertical-align="middle">
		    	<rect x="{$x}" y="{$y}" width="{$width}" height="{$height}"/>
		    </flowRegion>
		    <flowDiv>
		      	<flowPara justification="middle" fill="white" font-weight="bold" font-size="{$height * 0.75}"><xsl:value-of select="$quantity"/></flowPara>
		    </flowDiv>
		</flowText>			
  	</xsl:template>
  	
  	<xsl:template match="*" mode="render-image">
  		<xsl:param name="x"/>
  		<xsl:param name="y"/>
  		<xsl:param name="image-width"/>
  		<xsl:param name="image-height"/>
		<xsl:choose>
			<xsl:when test="@width">
				<xsl:variable name="scale-x" select="$image-width div @width"/>
				<xsl:variable name="scale-y" select="$image-height div @height"/>
				<xsl:variable name="scale">
					<xsl:choose>
						<xsl:when test="$scale-x > $scale-y">
							<xsl:value-of select="$scale-y"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$scale-x"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:variable name="scaled-image-width" select="$scale * @width"/>
				<xsl:variable name="scaled-image-height" select="$scale * @height"/>
			
				<g transform="translate({$x + ($image-width - $scaled-image-width) div 2}, {$y + ($image-height - $scaled-image-height) div 2})">
					<g transform="scale({$scale})">
						<xsl:copy-of select="*"/>			
					</g>
				</g>
			</xsl:when>
			<xsl:otherwise>
				<rect x="{$x}" y="{$y}" width="{$image-width}" height="{$image-height}" fill="white" stroke="black"/>
			</xsl:otherwise>
		</xsl:choose>
  	
  	</xsl:template>
  	
  	<xsl:template match="*" mode="render-description">
  		<xsl:param name="x">0</xsl:param>
  		<xsl:param name="y">0</xsl:param>
  		<xsl:param name="width"/>
  		<xsl:param name="height"/>
  		<xsl:param name="title"/>
  		<!-- should be using this (SVG 1.2)
			
			<flowRoot>
    			<flowRegion>	
      				<rect x="0" y="0" width="{$card-width}" height="{$card-height - $text-offset}"/>
    			</flowRegion>
      			<flowPara>Tomorrow, and tomorrow, and tomorrow; creeps in this 
       			petty pace from day to day, until the last syllable of recorded time. 
       			And all our yesterdays have lighted fools the way to dusty death.</flowPara>
  			</flowRoot>
			 
			-->
			
  		
		<flowText xmlns="http://xml.apache.org/batik/ext">
			<flowRegion>
		    	<rect x="{$x}" y="{$y}" width="{$width}" height="{$height}"/>
		    </flowRegion>
		    <flowDiv>
		    	<xsl:if test="$title">
		      		<flowPara font-weight="bold">
		      			<xsl:value-of select="$title"/>
		      		</flowPara>
		      	</xsl:if>
		      	<!-- TODO handle paragraph tags -->
		      	<flowPara top-margin="{$minor-spacing}" justification="left" font-style="italic"><xsl:value-of select="."/></flowPara>
		    </flowDiv>
		</flowText>			
  	</xsl:template>
  	
  	<xsl:template match="deck" mode="count-cards">
  		<xsl:param name="item-index" select="count(item)"/>
  		<xsl:param name="count" select="count(knowledge)"/>
  		<xsl:choose>
  			<xsl:when test="$item-index > 0">
  				<xsl:variable name="item-count">
  					<xsl:apply-templates select="item[$item-index]" mode="count-cards"/>
  				</xsl:variable>
  				<xsl:apply-templates select="." mode="count-cards">
  					<xsl:with-param name="item-index" select="$item-index - 1"/>
  					<xsl:with-param name="count" select="$count + $item-count"/>
  				</xsl:apply-templates>
  			</xsl:when>
  			<xsl:otherwise>
  				<xsl:value-of select="$count"/>
  			</xsl:otherwise>
  		</xsl:choose>
  	</xsl:template>
 
 	<xsl:template match="item" mode="count-cards">
		<xsl:variable name="item-spec-count">
			<xsl:apply-templates select="item-spec" mode="count-cards"/>
		</xsl:variable>
		<xsl:value-of select="$item-spec-count * @quantity"/>
 	</xsl:template>
 	
 	<xsl:template match="item-spec" mode="count-cards">
 		<xsl:value-of select="sum(application/action/@quantity) + 1"/>
 	</xsl:template>
 	
</xsl:stylesheet>